package cn.spdb.harrier.common.test;


import javax.swing.*;
import javax.swing.Timer;
import java.time.*;
import java.util.*;
import java.text.*;
import java.awt.*;
import java.awt.geom.AffineTransform;
public class StillClock extends JPanel{
    public static void main(String[] args){
        JFrame frame = new JFrame("搬砖时钟");
        frame.setTitle("努力搬砖ing");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setType(Window.Type.UTILITY);
        StillClock clock = new StillClock();
        frame.getContentPane().add(clock);
        frame.setLocation(1300,10);
        frame.setSize(300,350);
        frame.setVisible(true);
        Timer timer = new Timer(1000,e->{
           clock.repaint();
        });
        timer.start();
    }
    public void paintComponent(Graphics g){
        Graphics2D g2d = (Graphics2D)g;
//去锯齿
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//清空原来的图形
        g2d.setColor(Color.WHITE);
        g2d.fillRect(0, 0, this.getWidth(), this.getHeight());
        g2d.setColor(Color.BLACK);
//圆中心坐标
        int xCenter = this.getWidth() / 2;
        int yCenter = this.getHeight() / 2;
// 计算半径
        int radius = (int) (Math.min(this.getWidth(), this.getHeight()) * 0.8 * 0.5)+15;
// 画圆
        g2d.drawOval(xCenter - radius, yCenter - radius, radius * 2, radius * 2);
//画时钟的12个数字(如果用rotate方法则数字会倾斜倒立)
        for (int i = 0; i < 12; i++)
        {
//已知圆中心占坐标(x0,y0),半径r，角度a0,则圆上任一点坐标（x,y）计算：
//x = x0 + r * cos(ao * 3.14 /180)
//y = y0 + r * sin(ao * 3.14 /180)
            double dd = Math.PI / 180 * i * (360 /12); //每次转360/12度
            int x = (xCenter-4) + (int)((radius - 12) * Math.cos(dd));
            int y = (yCenter+4) + (int)((radius - 12) * Math.sin(dd));
//因为是从顺时钟3点钟开始画，所以索引i需要加上3
            int j = i + 3;
            if (j > 12)
                j = j - 12;
            g2d.drawString(Integer.toString(j), x, y);
        }
        AffineTransform old = g2d.getTransform();
//时钟的60个刻度
        for (int i = 0; i < 60; i++)
        {
            int w = i % 5 == 0 ? 5 : 3;
            g2d.fillRect(xCenter-2, 28, w, 3);
            g2d.rotate(Math.toRadians(6), xCenter, yCenter);
        }
//设置旋转重置
        g2d.setTransform(old);
        //设置实时北京时间
        Calendar now = Calendar.getInstance();
        int hour = now.get(Calendar.HOUR_OF_DAY);
        int minute = now.get(Calendar.MINUTE);
        int second = now.get(Calendar.SECOND);
        //计算下班倒计时
        LocalDateTime localNow = now.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        LocalDateTime offWork = LocalDateTime.of(LocalDate.now(), LocalTime.of(18,30,1));
        Duration duration = Duration.between(localNow,offWork);
        long offWorkHour = duration.toHours();
        duration=duration.minusHours(offWorkHour);
        long offWorkMinute = duration.toMinutes();
        duration=duration.minusMinutes(offWorkMinute);
        long offWorkSecond = duration.getSeconds();
        String echo = "(•́へ•́╬)";
        int echoX = xCenter -50;
        if(offWorkHour>0){
            echo += offWorkHour+"时";
            echoX = xCenter -80;
        }else {
            if (offWorkMinute <=30){
                echo="꒰╬•᷅д•᷄╬꒱";
            }
            if (offWorkMinute <=10){
                echo ="(╬◣д◢)";
            }
        }
        if (offWorkMinute>0){
            echo += offWorkMinute+"分";
            echoX = xCenter -70;
        }
        if (offWorkSecond>=0){
            echo += offWorkSecond+"秒";
        }else {
            echoX = xCenter-50;
            echo = "ヾ(◍°∇°◍)ﾉﾞ溜了溜了！";
        }

// 画时钟的图形
        double hAngle = (hour - 12 + minute / 60d) * 360d / 12d;
        g2d.rotate(Math.toRadians(hAngle), xCenter, yCenter);
        int xhArr[] = { xCenter, xCenter+9, xCenter, xCenter-9 };
        int yhArr[] = { 65, yCenter, yCenter+10, yCenter};
        g2d.drawPolygon(xhArr, yhArr, xhArr.length);
        g2d.setTransform(old);
// 画分钟的图形
        double mAngle = (minute + second / 60d) * 360d / 60d;
        g2d.rotate(Math.toRadians(mAngle), xCenter, yCenter);
        int xmArr[] = { xCenter, xCenter+6, xCenter, xCenter-6 };
        int ymArr[] = { 45, yCenter, yCenter+12, yCenter};
        g2d.drawPolygon(xmArr, ymArr, xmArr.length);
        g2d.setTransform(old);
// 画秒钟的图形
        double sAngle = second * 360d / 60d;
        g2d.rotate(Math.toRadians(sAngle), xCenter, yCenter);
        g.drawLine(xCenter, yCenter+10, xCenter, 35);
        g2d.setTransform(old);

        //画日期和星期
        DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
        DateFormat df2 = new SimpleDateFormat("E");
        g2d.drawString(df1.format(now.getTime()), xCenter-55, yCenter-65);
        g2d.drawString(df2.format(now.getTime()), xCenter-12, yCenter-50);
        g2d.setColor(Color.red);
        g2d.drawString(echo,echoX,yCenter-80);
    }
}